<!---
 @Name: index.vue
 @Author: lhf
 @Owner:
 @Date: 2025-07-19 15:06:42
 @LastEditTime: 2025-07-19 15:06:42
 @LastEditors:lhf
 @Description:
 @Version:
 @FilePath: src\views\floor\index.vue
--->

<template>
  <div class="category-container">
    <!-- 左侧分类 -->
    <div class="sidebar">
      <ul>
        <li v-for="(cat, index) in categories" :key="`${index}`" :class="{ active: index === activeIndex }" @click="onCategoryClick(index)">
          {{ cat.name }}
        </li>
      </ul>
    </div>

    <!-- 右侧商品详情 -->
    <div class="content" ref="content">
      <div v-for="(cat, index) in categories" :key="`${index}`" :ref="'section' + index" class="section">
        <h3 class="floor-header">{{ cat.name }}</h3>
        <div class="floor-body">
          <span v-for="(item, index) in cat.items" :key="`${index}`" class="item"> {{ item }} </span>
        </div>
        <div class="floor-footer" v-if="cat.picUrl">
          <img :src="cat.picUrl || ''" alt="" @click="showImageAction(cat)" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getProgramInfo } from '@/api';

export default {
  name: 'CategoryView',
  props: {
    programId: String
  },
  data() {
    return {
      activeIndex: 0,
      lists: [],
      sectionTops: []
    };
  },
  computed: {
    categories() {
      return this.lists.map((item) => {
        const { content = '', ...rest } = item;
        const items = content.split(',');
        return {
          ...rest,
          items: items
        };
      });
    }
  },
  mounted() {
    if (this.programId?.trim()) {
      this.getProgramInfos();
    }
  },
  methods: {
    showImageAction(row) {
      this.$AndroidSdk.ImageAction(row.url);
    },
    onCategoryClick(index) {
      this.activeIndex = index;
      const el = this.$refs['section' + index]?.[0];
      const container = this.$refs.content;
      try {
        container.scrollTo({ top: el.offsetTop, behavior: 'smooth' });
      } catch (e) {
        container.scrollTop = el.offsetTop;
      }
    },
    getProgramInfos() {
      getProgramInfo({
        programId: this.programId
      }).then((res) => {
        if (res.status == 0) {
          this.lists = res.data || [];
        }
      });
    }
  }
};
</script>
<style scoped lang="scss">
.category-container {
  display: flex;
  height: 100%;
  .sidebar {
    width: 296px;
    background: #fff;
    overflow-y: auto;
    border-radius: 20px;
    border: 2px solid #ddd;
    li {
      height: 120px;
      line-height: 120px;
      background: #fff;
      font-weight: 600;
      font-size: 42px;
      color: #333;
      text-align: center;
      padding: 0 10px 0 20px;
      border-left: 10px solid transparent;
      &.active {
        background: #ccdef0;
        color: #0059b3;
        border-left: 10px solid #0059b3;
      }
    }
  }
  .content {
    flex: 1;
    overflow: auto;
    -webkit-overflow-scrolling: touch; /* iOS 惯性滚动 */
    scroll-behavior: smooth; /* 平滑滚动 */
    padding: 30px 30px 30px 45px;
    margin-left: 30px;
    background: #fff;
    border-radius: 20px;
    border: 2px solid #ddd;
    position: relative;
    .floor-header {
      position: relative;
      padding-left: 30px;
      font-size: 45px;
      color: #333;
      line-height: 120px;
      &:after {
        position: absolute;
        top: 40px;
        left: 0;
        display: table;
        content: '';
        height: 45px;
        width: 10px;
        background: #0059b3;
      }
    }
    .floor-body {
      margin: 0 -15px;
      span {
        display: inline-block;
        vertical-align: top;
        height: 80px;
        line-height: 80px;
        background: #f0f5f9;
        border-radius: 10px;
        font-size: 42px;
        color: #333;
        margin: 15px;
        padding: 0 25px;
      }
    }
    .floor-footer {
      img {
        display: block;
        object-fit: cover;
        width: 900px;
      }
    }
  }
}
</style>
